<?php

namespace Events;

use Nette;
use Nette\Application\UI\Control;

use Nette\ArrayHash;
use Nette\DateTime;

class EventsForm extends Control
{





    /**
     * mrdka_z_krtka
     *
     * use Nette\ArrayHash;
     *use Nette\DateTime;
     */
	public function mrdka_z_krtka()
        {
            $file = fopen( __DIR__ . '/import.csv', 'r');

            $results = array();
            while (($data = fgetcsv($file, 1000, ';')) !== FALSE) {
                    $results[] = $data;
            }
            fclose($file);

            $params = $results[0];
            unset($results[0]);

            $data = array();
            $j = 0;
            foreach($results as $result) {
                    $data[$j] = array();
                    for($i = 0; $i < count($params); $i++) {
                            $param = $params[$i];
                            $data[$j][$param] = $result[$i];
                    }
                    $j++;
            }

            $items = ArrayHash::from($data);

            foreach ($items as $item) {
                /***************************************************************************************
                 * client
                 **/
                $client = new ArrayHash;
                $client->registered           = new DateTime;
                $client->modified             = NULL;
                $client->user                 = (int) $item->user;
                $client->typeCompany          = $item->typeCompany != '' ? $item->typeCompany : NULL;
                $client->nameCompany          = $item->nameCompany != '' ? $item->nameCompany : NULL; // => "str"
                $client->degree               = $item->degree != '' ? (int) $item->degree : NULL; // => "int"
                $client->name                 = $item->name != '' ? $item->name : NULL; // => "Svatopluk" (9)
                $client->surname              = $item->surname != '' ? $item->surname : NULL; // => "Praum" (5)
                $client->identificationNumber = NULL;
                $client->ico                  = NULL;
                $client->dic                  = NULL;
                $client->age                  = NULL;
                $client->phone                = $item->phone; // => "608954993" (9)
                $client->email                = $item->email; // => "email"
                if($client->name || $client->surname) {
                    $client->flag = 'physical';
                    if($item->sex == 1) {
                          $client->sex = 'male';
                    } else {
                          $client->sex = 'female';
                    }
                } else {
                    $client->flag = 'legal';
                    $client->sex = NULL;
                }

                /***************************************************************************************
                 * clients note
                 **/
                if(isset($item->note)){
                    $note = new ArrayHash;
                    //$note->client =
                    $note->created  = new DateTime;
                    $note->modified = NULL;
                    $note->text     = $item->note;
                }

                /***************************************************************************************
                 * contract
                 */
                $contract = new ArrayHash;
                // $contract->client
                $contract->user        = (int)$item->user; // => "31" (2)
                $contract->registered  = new DateTime;
                $contract->beginning   = $item->beginning != '' ? DateTime::from($item->beginning) : NULL; // => "2011-04-01" (10)
                $contract->validFrom   = $item->validFrom != '' ? DateTime::from($item->validFrom) : NULL; // => "2049-03-30" (10)
                $contract->modified    = NULL;
                $contract->product     = (int) $item->product; // => "145" (3)
                $contract->number      = (int) $item->number; // => "13305265" (8)
                if($contract->beginning != NULL && isset($item->ending)){
                    //$contract->length = $contract->beginning->diff(DateTime::from($item->ending)->format('%m');
                } else {
                    $contract->length = 0;
                }

                $contract->frequency   = (int) $item->frequency; // => "4"
                $contract->amount      = (int) $item->amount; // => "13 932 " (8)
                // $contract->cancel (no|yes)
                // $contract->flag (new|transition)

                /***************************************************************************************
                 * contract point
                 */
                $point = new ArrayHash;
                $point->insert     = new DateTime('now');
                $point->update     = NULL;
                // $point->contract =
                $point->points     = $item->points != '' ? ($item->points) : NULL;;
                // $point->commission = $points->point * sazba za bod
                $point->honored    = isset($item->honored) ? ($item->honored == 1 ? 'yes' : 'no') : 'no';

                /***************************************************************************************
                 *
                 * TO CO V TABULCE ZŮSTALO
                 *
                 */
                /*
                 $contracts->= $item->person; // => "1"
                 $contracts->= $item->insuranceCompany; // => "Poj. ČS" (8)
                 $contracts->= $item->mesicVypojistkovani; // => "storno pro neplacení" (21)
                 $contracts->= $item->stornofond; // => "$0 " (3)
                 */
dump($client->sex, $client->name, $client->surname/*, $note, $contract, $point*/); //exit;
            }
        }

	public function render()
	{
		$this->template->setFile(__DIR__ . '/eventsForm.latte');
		$this->template->render();
		$this->mrdka_z_krtka();
	}
}